package smlab.view.ui.tree
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/**
	 * 树形组件
	 * @author simon
	 * 
	 */
	public class TreeNode_SM extends Sprite
	{
		protected var nodeState:String=Tree_SM.NODE_STATE_CLOSE;//当前节点的开关状态
		private var data_:Object=new Object;//当前节点数据
		private var childNodes_:Array=[];//当前节点的子节点
		private var parentNode_:TreeNode_SM;//当前节点的父节点
		private var skin_:DisplayObject;//当前节点的 UI
		private var childContainer:Sprite;//子节点容器
		private var selected_:Boolean=false;//子节点容器
		public var title:String="";//当前节点的文字标题
		public var id:int;//节点的唯一标识id
		public var level:uint=0;//当前节点深度,从0开始
		public var xGap:Number=0;//当前节点的x轴缩进
		public var yGap:Number=0;//当前节点的y轴缩进
		
		public function TreeNode_SM()
		{
			super();
			addEventListener(MouseEvent.CLICK,onClick);
			addEventListener(TreeEvent_SM.CLICK_NODE,onOtherNodeClick);
		}
		
		public function set skin(d:DisplayObject):void
		{
			skin_=d;
			addChild(skin_);
		}
		
		public function get skin():DisplayObject
		{
			return skin_;
		}
		
		public function set data(d:Object):void
		{
			data_=d;
			updata();
		}
		
		public function get data():Object
		{
			return data_;
		}
		
		public function set selected(b:Boolean):void
		{
			selected_=b;
		}
		
		public function get selected():Boolean
		{
			return selected_;
		}
		
		/**
		 * 添加一个子节点
		 * @param n
		 * 
		 */
		public function addChildNode(n:TreeNode_SM):void
		{
			childNodes_.push(n);
		}
		
		/**
		 * 删除一个子节点
		 * @param id
		 * 
		 */
		public function removeChildNode(id:int):void
		{
			for(var i:int=0;i<childNodes_.length;i++){
				var treeNode:TreeNode_SM=childNodes_[i];
				
				if(treeNode.id==id){
					childNodes_.splice(i,1);
					drawLayout();
					return;
				}
			}
		}
		
		/**
		 * 获取所有子节点
		 * @return 
		 * 
		 */
		public function get childNodes():Array
		{
			return childNodes_;
		}
		
		
		/**
		 * 获取所有子节点
		 * @return 
		 * 
		 */
		public function set childNodes(a:Array):void
		{
			childNodes_=a;
		}
		
		/**
		 * 获取父级节点
		 * @return 
		 * 
		 */
		public function get parentNode():TreeNode_SM {
			return parentNode_;
		}
		
		public function set parentNode(value:TreeNode_SM):void {
			parentNode_ = value;
		}
		
		/**
		 * 初始化数据，一般需要 override
		 * 
		 */
		public function updata():void
		{
			if(skin is DisplayObjectContainer)
			{
				var t:TextField=DisplayObjectContainer(skin).getChildByName("txt") as TextField;
				
				if(t){
					t.mouseEnabled=false;
					t.text=title;
					t.height=t.textHeight+4;
				}
				
			}
		}
		
		/**
		 * 树形的所有节点被点击均执行该方法
		 * @param e
		 * 
		 */
		private function onOtherNodeClick(e:TreeEvent_SM):void
		{
			drawLayout();
		}
		
		/**
		 * 自身被点击
		 * @param e
		 * 
		 */
		private function onClick(e:MouseEvent):void
		{
			e.stopPropagation();
			
			if(nodeState==Tree_SM.NODE_STATE_CLOSE)
			{
				nodeState=Tree_SM.NODE_STATE_OPEN;
				open();
			}else{
				nodeState=Tree_SM.NODE_STATE_CLOSE;
				close();
			}
			if(e.currentTarget==this){
				dispatchEvent(new TreeEvent_SM(TreeEvent_SM.CLICK_NODE,this,true));
			}
		}
		
		
		/**
		 * 节点被点击后展开，一般需要 override
		 * 
		 */
		public function open():void
		{
			this.selected=true;
			childContainer.y = this.height;
			addChild(this.childContainer);
		}
		
		/**
		 * 节点被点击后关闭，一般需要 override
		 * 
		 */
		public function close():void
		{
			this.selected=false;
			removeChildContainer();
		}
		
		private function removeChildContainer():void
		{
			if(this.contains(this.childContainer)){
				this.removeChild(this.childContainer);
			}
		}
		
		/**
		 *绘制子节点显示对象
		 * 放入容器中childrenContainer
		 * 这个容器暂时不显示
		 *
		 */
		public function drawChildrenDisplay():void
		{
			childContainer = new Sprite();
			
			for (var i:int = 0; i < this.childNodes.length; i++) {
				var childNode:TreeNode_SM = this.childNodes[i];
				childContainer.addChild(childNode);
			}
			drawLayout();
		}
		
		/**
		 *设置位置
		 *
		 */
		protected function drawLayout():void
		{
			if (childNodes.length<=0)
			{
				removeChildContainer();
				return ;
			}
			
			for (var i:int = 0; i < childNodes.length; i++)
			{
				var childNode:TreeNode_SM = childNodes[i];
				childNode.x=childNode.xGap;
				
				if (i == 0) {
					childNode.y = yGap;
				} else {
					childNode.y = childNodes[i - 1].y + childNodes[i - 1].height+yGap;
				}
			}
		}
	}
}